package com.gem.controller;

import com.gem.domain.ClassInfo;
import com.gem.domain.Student;
import com.gem.service.StudentService;
import com.gem.util.ConditionUtil;
import com.gem.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

/**
 * @author Apolar_GuanYH-管阳红
 * Description: 学生控制器
 * create 2019/11/6 9:32
 * Modified By:
 */
@Controller
@RequestMapping("/stu")
public class StudentController {

    private StudentService studentService;

    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    //显示信息
    @RequestMapping("/stuShow")
    public String stuShow(Model model, String classId,ClassInfo classinfo,String classDate,
                          @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                          @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            String classType = classinfo.getSName();
            String classTeacher = classinfo.getTName();
            String classHeadTeacher = classinfo.getMName();
            List<Student> stuList = studentService.selectAllStuByClassService(Integer.parseInt(classId));
            if (stuList.size() > 0) {
                model.addAttribute("stuList", stuList);
            } else {
                model.addAttribute("info", "暂无信息记录");
            }
            model.addAttribute("classId",classId);
            model.addAttribute("currentPage",currentPage);
            model.addAttribute("pageSize", pageSize);
            model.addAttribute("classDate", classDate);
            model.addAttribute("classType", classType);
            model.addAttribute("classHeadTeacher", classHeadTeacher);
            model.addAttribute("classTeacher", classTeacher);
            return "career/studentInfo";
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/home", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //查询显示
    @RequestMapping("/stuSearchShow")
    public String stuSearchShow(Model model, ConditionUtil cu,ClassInfo classinfo,String classDate,
                                @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                @RequestParam(value = "pageSize", defaultValue = "5") int pageSize){
        try {
            String classType = classinfo.getSName();
            String classTeacher = classinfo.getTName();
            String classHeadTeacher = classinfo.getMName();
            List<Student> stuList = studentService.selectStuByCondition(cu);
            if (stuList.size() > 0) {
                model.addAttribute("stuList", stuList);
            } else {
                model.addAttribute("info", "暂无信息记录");
            }
            model.addAttribute("sc",cu);
            model.addAttribute("classId",cu.getClassId());
            model.addAttribute("classDate", classDate);
            model.addAttribute("classType", classType);
            model.addAttribute("classHeadTeacher", classHeadTeacher);
            model.addAttribute("classTeacher", classTeacher);
            return "career/studentInfo";
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/home", "服务器异常，请联系管理员哦！", model);
        return "message";
    }
}
